//
//  WDCustomSheet.m
//  WDCustomView
//
//  Created by WD on 2016/11/24.
//  Copyright © 2016年 WD. All rights reserved.
//

#import "WDCustomSheet.h"
#import "UIImage+WDExtension.h"
#define btnH 40
#define marginH 79
#define fontSize 15
#define Duration 0.5
#define ContentMaxH (79 + 40 * 3)
#define CurrentH (40 * allbus.count + 79)

@interface WDCustomSheet()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong) UITableView * contentView;
@property (nonatomic, strong) UILabel *titleLabel;
/** 是不是tableViewController */

@end
@implementation WDCustomSheet

static NSArray * allbus = nil;
static BOOL Original = NO;
static NSString *cellID = @"btnCell";

-(WDCustomSheet*)initWithButtons:(NSArray*)allButtons isOriginal:(BOOL)isOriginal
{
    allbus = allButtons;
    
    Original = isOriginal;
    
    WDCustomSheet * sheet = [[WDCustomSheet alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [sheet set];
    return sheet;
}

/** 动画 */
-(void)set
{
    CGFloat margin;
    if (Original) {
        
        margin = 44;
        
    } else {
        
        margin = 0;
    }
    if (CurrentH <= ContentMaxH) {
        
        [UIView animateWithDuration:Duration animations:^{
            _contentView.frame = CGRectMake(0, WDScreenH - margin - CurrentH, WDScreenW, CurrentH);
        }];
    } else {
        
        [UIView animateWithDuration:Duration animations:^{
            _contentView.frame = CGRectMake(0, WDScreenH - margin - ContentMaxH, WDScreenW, ContentMaxH);
        }];
    }
    
    

}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        back.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
        [self addSubview:back];
        
        if (CurrentH <= ContentMaxH) {
            
            _contentView = [[UITableView alloc]initWithFrame:CGRectMake(0, WDScreenH,  WDScreenW, CurrentH) style:UITableViewStylePlain];
            
        } else {
            
            _contentView = [[UITableView alloc]initWithFrame:CGRectMake(0, WDScreenH,  WDScreenW, ContentMaxH) style:UITableViewStylePlain];
            
        }
        
        _contentView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _contentView.delegate = self;
        _contentView.dataSource = self;
        //注册cell;
        [_contentView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
        _contentView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:_contentView];
    
    }
    
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allbus.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.textLabel.text = allbus[indexPath.row];
    cell.textLabel.textColor = ColorForRGB(0x6b6b6b);
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, marginH)];
    
    headV.backgroundColor = [UIColor whiteColor];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width * 0.5, btnH - 1)];
    _titleLabel.textColor = ColorForRGB(0x4c4c4c);
    _titleLabel.font = [UIFont systemFontOfSize:fontSize];
    _titleLabel.text = self.title;
    [headV addSubview:_titleLabel];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, btnH - 1, [UIScreen mainScreen].bounds.size.width, 1)];
    line.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    [headV addSubview:line];
    
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return btnH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return btnH;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor orangeColor];
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = ColorForRGB(0x6b6b6b);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor orangeColor];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate  customSheet:self clickButton:indexPath.row];
    
    [self removeFromSuperview];
}


@end
